package com.scalar.chiptrack.operations.reports;

import com.scalar.chiptrack.operations.reports.dao.WipFlushDAO;
import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.utils.ConfigManager;
import com.scalar.chiptrack.utils.Mailer;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.users.dao.NotificationSetupDAO;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.ScalarException;

import java.util.ArrayList;
/**
 * Author: Varma
 * Date: 28 Oct 2005
 * Description: To add the Wip Flush PDF, Excel Mail attachments as a Job.
 */

public class WipFlushMailingJob extends JobHandler
{
    public void execute() throws Exception
    {
        try
        {
            String[] mailAttachments = null;
            String[] emailArray = null;
            try
            {
                emailArray = NotificationSetupDAO.loadEmailsByType( Constants.NOTIFICATION_EMAIL_TYPE_FOR_REPORTS );
            }
            catch ( ScalarException se )
            {
                se.getRootCause().printStackTrace();
                System.out.println( "Exception while retrieving email list" );
            }
            //Loading the Wip details from DB.
            ArrayList wipList = WipFlushDAO.getWipInventoryByProduct(null,0,1,"1", null);
            //Generating the PDF file
            String pdfFilename = WipFlushFileGenerator.getWipFlushAsPDFFile( wipList );
            //Generating the Excel File.
            String excelFilePath = WipFlushFileGenerator.getWipFlushAsExcelFile( wipList );

            String from = ConfigManager.getScalarAdminEmail();
            int index = from.lastIndexOf( "\\" );
            from = from.substring( index + 1, from.length() );
            String to = StringUtils.concatStrings( emailArray, Mailer.EMAIL_SEPARATOR );
            String subject = "WIP REPORT";
            //Setting the PDF and Excel files as attachments.
            mailAttachments = new String[]{pdfFilename, excelFilePath};

            Mailer.send( from, to, null, null, subject, null, mailAttachments, Mailer.CONTENT_TYPE_HTML, false );
        }
        catch( Exception e)
        {
            e.printStackTrace();
        }

    }
}